package com.twu.biblioteca;

/**
 * Created by olga on 7/06/14.
 */
public class User
{
    private String libraryNumber;
    private String password;

    private String firstName;
    private String lastName;
    private String email;
    private String phoneNumber;

    public User(String libraryNumber, String password)
    {
        setLibraryNumber(libraryNumber);
        this.password = password;

        this.firstName = "";
        this.lastName = "";
        this.email = "";
        this.phoneNumber = "";
    }

    public User(String libraryNumber, String password, String firstName, String lastName, String email, String phoneNumber)
    {
        setLibraryNumber(libraryNumber);
        this.password = password;

        setFullUserName(firstName, lastName);
        setEmail(email);
        setPhoneNumber(phoneNumber);
    }

    private void setLibraryNumber(String libraryNumber)
    {
        String regex = "([a-zA-Z0-9]{3})-([a-zA-Z0-9]{4})";
        this.libraryNumber = (libraryNumber.matches(regex)) ? libraryNumber : "";
    }

    public String getLibraryNumber()
    {
        return this.libraryNumber;
    }
    protected String getPassword() { return this.password; }

    public String getFullUserName()
    {
        return this.firstName + " " + this.lastName;
    }

    public void setFullUserName(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getEmail()
    {
        return this.email;
    }

    public void setEmail(String email)
    {
        String regex = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$";
        this.email = ( email.matches(regex) ) ? email : "";
    }

    public String getPhoneNumber()
    {
        return this.phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber)
    {
        this.phoneNumber = phoneNumber;
    }

    public void printUserDetails()
    {
        TerminalPrintUtility.printStringWithCarriageReturn("Library Number: " + this.getLibraryNumber());
        TerminalPrintUtility.printStringWithCarriageReturn("Name: " + this.getFullUserName());
        TerminalPrintUtility.printStringWithCarriageReturn("Email: " + this.getEmail());
        TerminalPrintUtility.printStringWithEmptyLine("Phone: " + this.getPhoneNumber());
    }

    @Override
    public boolean equals(Object obj)
    {
        if( obj instanceof User )
        {
            return ((User)obj).getLibraryNumber().equalsIgnoreCase(this.getLibraryNumber()) &&
                   ((User)obj).getPassword().equals(this.getPassword());
        }

        return false;
    }
}
